package com.sx.micowell.framework.model

import com.example.mvvm.base.data.SingleLiveEvent
import com.example.mvvm.base.vm.BaseViewModel
import com.example.mvvm.until.LogUtils
import com.example.mvvm.until.ToastUtils
import com.miloyu.modelnet.network.ApiResponse
import com.miloyu.modelnet.network.HttpClient
import com.miloyu.modelnet.network.tools.RxUtils
import com.miloyu.modelnet.response.FileInfoVO
import com.miloyu.modelnet.response.UserInfoVO
import com.sx.micowell.framework.binding.BindingCommand
import com.sx.micowell.framework.binding.impl.IBindingAction
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody
import java.io.File
import retrofit2.http.Field


/**
 * @Author: YY
 * @Time: 2021/8/19
 * @Describe TODO
 */
class PersonalInfoModel : BaseViewModel() {

    val userInfoEvent : SingleLiveEvent<UserInfoVO> = SingleLiveEvent()

    // 选择头像事件
    var selectHeadersEvent: SingleLiveEvent<Void> = SingleLiveEvent()
    // 昵称
    var nicknameEvent : SingleLiveEvent<Void> = SingleLiveEvent()
    // 性别
    var sexEvent: SingleLiveEvent<Void> = SingleLiveEvent()
    // 身高
    var heightEvent: SingleLiveEvent<Void> = SingleLiveEvent()
    // 目标体重
    var weightEvent: SingleLiveEvent<Void> = SingleLiveEvent()
    // 生日
    var birthdayEvent: SingleLiveEvent<Void> = SingleLiveEvent()

    // 选择头像
    val selectHeadersOnClickCommand: BindingCommand<Any> = BindingCommand(IBindingAction {
        LogUtils.d("selectHeadersOnClickCommand")
        selectHeadersEvent.call()
    })

    // 修改昵称
    val nickNameOnClickCommand: BindingCommand<Any> = BindingCommand(IBindingAction {
        nicknameEvent.call()
    })

    // 选择性别
    val sexSelectOnClickCommand: BindingCommand<Any> = BindingCommand(IBindingAction {
        sexEvent.call()
    })

    // 修改身高
    val heightOnClickCommand: BindingCommand<Any> = BindingCommand(IBindingAction {
        heightEvent.call()
    })

    // 目标体重
    val weightOnClickCommand: BindingCommand<Any> = BindingCommand(IBindingAction {
        weightEvent.call()
    })

    val birthdayOnClickCommand: BindingCommand<Any> = BindingCommand(IBindingAction {
        birthdayEvent.call()
    })

    fun userInfo() {

        addSubscribe(  HttpClient.instance.apiService
            .userInfo()
            .compose(RxUtils.compose())
            .subscribeWith(object : ApiResponse<UserInfoVO>() {

                override fun success(data: UserInfoVO?, msg: String?) {
                    LogUtils.d("userInfo: ${data?.userPhone}")
                    userInfoEvent.value = data
                    ui.refreshEvent.value = true
                }

                override fun onFail(e: Throwable?, errorMsg: String?) {
                    ToastUtils.showShort("$errorMsg")
                    ui.refreshEvent.value = false
                }

            })
        )

    }


    /**
     * 上传文件
     * @param fileName
     * @param file
     */
    fun uploadFile(fileName: String?, file: File?) {
        val fileRequestBody = RequestBody.create(
            "multipart/form-data".toMediaTypeOrNull(),
            file!!
        )
        val requestImgPart = MultipartBody.Part.createFormData("file", fileName, fileRequestBody)
        addSubscribe(
            HttpClient.instance.apiService
                .uploadFile(requestImgPart)
                .doOnSubscribe { ui.showDialog() }
                .compose(RxUtils.compose())
                .subscribeWith(object : ApiResponse<FileInfoVO>() {

                    override fun success(data: FileInfoVO?, msg: String?) {
                        LogUtils.d("uploadFile: " + data?.fileName)
                        modifyUserInfo(data?.fileName.toString(),
                            "","","","","")
                    }

                    override fun onFail(e: Throwable?, errorMsg: String?) {
                        LogUtils.d("uploadFile: ${e.toString()}")
                        ToastUtils.showShort("上传失败")
                        ui.hideDialog()
                    }
                })
        )
    }

    /**
     * 修改头像、昵称
     */
    fun modifyUserInfo(headUrl: String, nickname: String, userSex: String,
                       userBirthday: String, userHeight: String, userWeight: String) {
        addSubscribe(
            HttpClient.instance.apiService
                .modifyUserInfo(headUrl, nickname,userSex,userBirthday,userHeight,userWeight)
                .compose(RxUtils.compose())
                .subscribeWith(object : ApiResponse<Void>() {

                    override fun success(data: Void?, msg: String?) {
                        LogUtils.d("modifyUserInfo: ")
                        ToastUtils.showShort("修改成功")
                        userInfo()
                        ui.hideDialog()
                    }

                    override fun onFail(e: Throwable?, errorMsg: String?) {
                        LogUtils.d("modifyUserInfo: $errorMsg")
                        ToastUtils.showShort("修改失败")
                        ui.hideDialog()
                    }
                })
        )
    }
}